package beans;

import java.util.Map;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;

import utils.Navigation;
import dto.PartyDTO;
import entitymanagers.PartyManager;


@ManagedBean(name = "partyBean")
@RequestScoped
public class PartyBean {
	
	private Part file;
	
	@EJB
	private PartyManager partyMgr;
	
	private PartyDTO party;
	private PartyDTO[] allParties;
	private Part partySymbol;
	
	private String partyProgram;
	
	@ManagedProperty("#{param.partyID}")
	private String partyID;
	
	
	public String getPartyID(){
		if (partyID == null){
			return "";
		}
		return partyID;
	}
	
	public void setPartyID(String partyID){
		if (this.partyID == null){
			this.partyID = partyID;
		}
	}
	
	public PartyDTO getParty(){
		if (party == null){
			party = partyMgr.getPartiesByID(getPartyID());
							
		}return party;
	}
	
	public PartyDTO getParty(String partyID){
		if (partyID != null){
			return partyMgr.getPartiesByID(partyID);
					
		}return getParty();
	}
	
	public PartyDTO getParamParty(){
		if (partyID != null && party == null){
			party = partyMgr.getPartiesByID(partyID);
					
		}return party;
	}
	
	public PartyDTO[] getAllParties(){
		if (allParties == null){
			allParties = partyMgr.getParties();
		}
		return allParties;
	}
	
	public Part getFile(){
		return file;
	}
	
	public void setFile(Part file){
		this.file = file;
	}
	
	public String showResult(){
		FacesContext fc = FacesContext.getCurrentInstance();
		Map<String,String> params = 
				fc.getExternalContext().getRequestParameterMap();
		partyID = params.get("partyID");
		return Navigation.SHOW_PARTY;
	}

	public Part getPartySymbol() {
		return partySymbol;
	}

	public void setPartySymbol(Part partySymbol) {
		this.partySymbol = partySymbol;
	}

	public String getPartyProgram() {
		return partyProgram;
	}

	public void setPartyProgram(String partyProgram) {
		this.partyProgram = partyProgram;
	}
	
}
